package com.hb.commons;

/**
 * 枚举类
 * 消息提醒信息
 *
 * @author zhaojk
 */
public enum MsgEnum {
    /* 系统错误 */
    PROCESSES_IS_DUE(500, "后台异常", false),
    NO_AUTHORIZATION(403, "无访问或操作权限", false),
    REQUEST_NOT_FOUND(404, "请求地址不存在", false),
    INCORRECT_PARAM(400, "缺少必要参数", false),
    UNKNOWN_EXCEPTION(3002, "未知的错误类型", false),

    /* Session会话相关 */
    USER_WITHOUT_ANY_ROLE(2013, "请先联系管理员为该用户设置角色", false),
    USER_DUPLICATE_LOGIN(2012, "您的账号已在其他地点登录", false),
    USER_FORCE_OFFLINE(2011, "您已被强制下线", false),
    USER_DISABLE(2010, "用户已被禁用", false),
    PASSWORD_EXCEPTION(2009, "两次输入的密码不一致", false),
    USERNAME_NOT_ALLOW_EMPTY(2008, "用户名不能为空", false),
    PASSWORD_NOT_ALLOW_EMPTY(2007, "密码不能为空", false),
    LOGIN_EXPIRE(2006, "未登录或登陆已经过期, 请重新登录", false),
    LOGIN_ERROR(2005, "未能获取当前登录状态, 请重新登录", false),
    USERNAME_DUPLICATE(2004, "用户名已经存在", false),
    USERNAME_NOT_FOUND(2003, "该用户不存在", false),
    WRONG_USERNAME_PASSWORD(2002, "用户名或密码错误", false),
    WRONG_PASSWORD(2013, "密码错误", false),
    VALID_CODE_DEPRECATED(2001, "验证码已经失效, 请重新操作", false),

    /* 业务逻辑 */
    NULL_RESULT(1006, "没有数据", true),
    ADMIN_NOT_ALLOW_DELETE(1005, "不能删除超级管理员", false),
    ADMIN_DUPLICATE(1004, "超级管理员有且必须只能有一个", false),
    ADMIN_ERROR(1003, "超级管理员的信息只能由本人修改", false),
    USER_ACCOUNT_FORBIDDEN(1002, "账号已被禁用", false),
    WRONG_VALID_CODE(1001, "验证码错误", false),

    FAILURE(0, "操作失败", false),
    SUCCESS(200, "操作成功", true);

    /**
     * 状态码
     */
    private final int code;

    /**
     * 提示消息
     */
    private final String message;

    /**
     * 接口调用标识
     */
    private final boolean flag;

    MsgEnum(int code, String message, boolean flag) {
        this.code = code;
        this.message = message;
        this.flag = flag;
    }

    public int code() {
        return code;
    }

    public String msg() {
        return message;
    }

    public boolean flag() {
        return flag;
    }

    /**
     * 根据代码获取消息提醒枚举类
     *
     * @param code
     * @return
     */
    public static MsgEnum codeOf(int code) {
        for (MsgEnum msgEnum : values()) {
            if (msgEnum.code() == code) {
                return msgEnum;
            }
        }
        return UNKNOWN_EXCEPTION;
    }
}
